// pages/publish/publish.js
var merchant_img = [];
var merchant_logo = '';
Page({

  data: {
    merchant_name: '',
    merchant_img: [],
    merchant_logo: '',
    merchant_phone: '',
    merchant_remark: '',
    merchant_text: '',
  },

  onLoad: function (options) {

  },
  onShow: function () {
    var value = wx.getStorageSync('user_id');
    if (value) {
      this.setData({
        user_id: value
      })
    } else {
      app.login()
        .then(app.doLogin)
        .then(app.checkAuth('scope.userInfo'))
        .then(app.getUserInfo)
        .then(app.setUserData)
        .catch(app.toLoad)
    }
  },
  bindNameInput: function (e) {
    this.setData({
      merchant_name: e.detail.value
    })
  },
  bindPhoneInput: function (e) {
    this.setData({
      merchant_phone: e.detail.value
    })
  },
  bindRemarkInput: function (e) {
    this.setData({
      merchant_remark: e.detail.value
    })
  },
  bindTextAreaBlur: function (e) {
    this.setData({
      merchant_text: e.detail.value
    })
  },
  // 发布帖子
  setPostData: function () {
    let that = this;
    var merchant_img_str = that.data.merchant_img.join(',');
    if (that.data.merchant_name == "") {
      wx.showToast({
        title: '商家名称不能为空！',
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.merchant_logo == "") {
      wx.showToast({
        title: '请上传logo！',
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.merchant_phone == "") {
      wx.showToast({
        title: '联系电话不能为空！',
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.merchant_text == "") {
      wx.showToast({
        title: '商家介绍不能为空！',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: 'https://rtutrip.aihuodong.top/index/index/set_merchant_data',
        data: {
          merchant_name: that.data.merchant_name,
          merchant_logo: that.data.merchant_logo,
          merchant_phone: that.data.merchant_phone,
          merchant_remark: that.data.merchant_remark,
          merchant_text: that.data.merchant_text,
          merchant_img: that.data.merchant_img
        },
        success(res) {
          console.log(res)
          wx.showToast({
            title: '您的入驻申请已提交，请等待审核！',
            icon: 'none',
            duration: 2000
          })
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/merchant/merchant',
            })
          }, 2000);
        }
      })
    }
  },
  uploadLogo: function () {
    var that = this;
    that.uploadImage().then(res => {
      console.log(res)
      const data = JSON.parse(res.data);
      if (data.ret == 200) {
        merchant_logo = data.data.path;
      }
      that.setData({
        merchant_logo: merchant_logo
      })
    })
  },
  upload: function () {
    var that = this;
    that.uploadImage().then(res => {
      console.log(res)
      const data = JSON.parse(res.data);
      if (data.ret == 200) {
        merchant_img.push(data.data.path);
      }
      that.setData({
        merchant_img: merchant_img
      })
    })
  },
  uploadImage: function () {
    return new Promise((resolve, reject) => {
      wx.chooseImage({
        count: 1,
        success(res) {
          const tempFilePaths = res.tempFilePaths
          wx.uploadFile({
            url: 'https://rtutrip.aihuodong.top/index/index/merchant_picture_upload',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
              'user': 'test'
            },
            success(res) {
              resolve(res)
            }
          })
        }
      })
    })
  }

})